package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.Equipment;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.EquipmentDao;
import com.efast.dmom.mes.datahandlers.base.PlantModelDataHandler;
import com.efast.dmom.mes.entity.EquipmentEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;
import com.efast.dmom.mes.util.TimeUtil;

/**
 * @author Aaron Liu
 *
 * @date:2020年2月4日
 */
@Component
@Transactional
public class EquipmentDataHandler extends PlantModelDataHandler<Equipment>{

	@Autowired
	private EquipmentDao equipmentDao;
	

	public void saveEquipmentList(List<Equipment> equipList) {

		if (equipList == null) {
			return;
		}

		equipmentDao.saveEquipmentList(equipList);
	}
	
	@Override
	public ResultBean save(Equipment obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		EquipmentEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		BaseEntityDataUtil.setBaseEntityData(entity);

		int save = equipmentDao.save(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		resultBean.setObject(obj);
		return resultBean;
	}
	
	@Override
	public ResultBean update(Equipment obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			equipmentDao.update(obj.getInnerData());
		}
		return resultBean;
	}
	
	@Override
	public ResultBean delete(Equipment obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			equipmentDao.delete(obj.getInnerData());
		}
		return resultBean;
	}

	public Equipment getById(long id) {
		Equipment equipment = new Equipment();
		equipment.setInnerData(equipmentDao.getById(id));
		return equipment;
	}

	public Equipment getByName(String name) {
		Equipment equipment = new Equipment();
		equipment.setInnerData(equipmentDao.getByName(name));
		return equipment;
	}


	public Equipment getByCode(String equipmentpCode) {
		Equipment equipment = new Equipment();
		equipment.setInnerData(equipmentDao.getByCode(equipmentpCode));
		return equipment;
	}

	@Override
	public ResultBean saveBatch(List<Equipment> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Equipment> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Equipment> obj) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List<Equipment> selectEquipBaseInfoData () {
		 List<EquipmentEntity> selectEquipBaseInfoData = equipmentDao.selectEquipBaseInfoData();
		 List<Equipment> list = new ArrayList<Equipment>();
		 if (selectEquipBaseInfoData.size()>0) {
			 for (EquipmentEntity equipmentEntity : selectEquipBaseInfoData) {
				Equipment equipment = new Equipment();
				equipment.setInnerData(equipmentEntity);
				list.add(equipment);
			}
		 }
		 return list;
	}
	
	public List<Equipment> selectEquipSpareDaoJuInfo() {
		List<Equipment> list = new ArrayList<Equipment>();
		List<EquipmentEntity> selectEquipSpareDaoJuInfo = equipmentDao.selectEquipSpareDaoJuInfo();
		if (selectEquipSpareDaoJuInfo.size()>0) {
			 for (EquipmentEntity equipmentEntity : selectEquipSpareDaoJuInfo) {
				Equipment equipment = new Equipment();
				equipment.setInnerData(equipmentEntity);
				list.add(equipment);
			}
		 }
		return list;
		
	}
	
	public  List<Equipment> selectEquipSpareDaoJuInfoByCode(String code) {
		
		List<Equipment> list = new ArrayList<Equipment>();
		List<EquipmentEntity> selectEquipSpareDaoJuInfo = equipmentDao.selectEquipSpareDaoJuInfoByCode(code);
		if (selectEquipSpareDaoJuInfo.size()>0) {
			 for (EquipmentEntity equipmentEntity : selectEquipSpareDaoJuInfo) {
				Equipment equipment = new Equipment();
				equipment.setInnerData(equipmentEntity);
				list.add(equipment);
			}
		 }
		return list;
		
	}
	
	public Equipment selectEquipSpareDaoJuInfoByDaoJuCode(String cutterCode) {
		EquipmentEntity selectEquipSpareDaoJuInfoByDaoJuCode = equipmentDao.selectEquipSpareDaoJuInfoByDaoJuCode(cutterCode);
		Equipment equipment = new Equipment();
		equipment.setInnerData(selectEquipSpareDaoJuInfoByDaoJuCode);
		return equipment;
	}
	
	public int updateStatusById(Long id,int status) {
		int updateStatusById = equipmentDao.updateStatusById(id,status);
		return updateStatusById;
	}

	public int updateStatusBySpareId(long spareId, int status) {
		int updateStatusById = equipmentDao.updateStatusBySpareId(spareId,status);
		return updateStatusById;
	}

}
